package demo01_hello_world;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

/**
 * @Description
 * @Author shihan
 * @Date 2020/12/19 22:11
 * @Version 1.0
 */
public class SimplePushConsumer {

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri("amqp://root:123456@192.168.66.129:5672/%2f");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        // 声明队列，没有则新创建一个；
        channel.queueDeclare("queue.qname",false,false,true,null);

        // 监听队列，队列有新消息时，触发回调函数
        channel.basicConsume("queue.qname",true, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                System.out.println("监听回调执行：" + new String(message.getBody()));
            }
        }, new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                // do nothing!
            }
        });

    }
}
